package org.algorithm.Thread;

public class ThreadSolution3 {

    static Object lock = new Object();
    static int start = 0;
    static int end = 100;

    public static void main(String[] args) {

        String[] arr = new String[3];
        arr[0] = "a";
        arr[1] = "b";
        arr[2] = "c";

        for (int i = 0; i < 3; i++) {
            new Thread(new Print(arr[i], i)).start();
        }
    }

    public static class Print implements Runnable {

        String character;
        int index;

        public Print(String character, int index) {
            this.character = character;
            this.index = index;
        }

        @Override
        public void run() {
            while (start<=end){
                synchronized (lock){
                    while (start%3!=index){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    if (start<=end){
                        System.out.println(character+"--->"+index+"--->"+Thread.currentThread().getId());
                        start++;
                    }
                    lock.notifyAll();
                }
            }
        }
    }

}
